package lwf.handler.math.algebra;

import java.util.List;

import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.number.util.NumberFormatter;
import net.lwf.number.util.RandomNumberGenerator;

import lwf.handler.IProcessHandler;
import lwf.handler.helper.DisplayFormatter;
import lwf.handler.helper.FormatAttribute;
import lwf.handler.math.ArithmaticsProcessHandler;



public class AlgebraProcessHandler extends ArithmaticsProcessHandler implements IProcessHandler {

	public static boolean isreverse = false;
	
	private String questionDesc = "";
	private String tempValue;
	private FormatAttribute format;
	
	public AlgebraProcessHandler(String datastring) {
		super(datastring);
		generateData();
	}
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		format = new FormatAttribute();
		
		format.setQuestionNumber(getQuestionNum());
		format.setQuestionTable(getQuestionDesc());
		format.setQuestionType(2); 
		
		return new DisplayFormatter(format);
	}

	/**
	 * @return Returns the tempValue.
	 */
	public String getTempValue() {
		return tempValue;
	}
	/**
	 * @param tempValue The tempValue to set.
	 */
	public void setTempValue(String tempValue) {
		this.tempValue = tempValue;
	}
	
	/**
	 * @return Returns the questiondesc.
	 */
	public String getQuestionDesc() {
		String format = NumberFormatter.getFormat(getDecimalDigits());
		StringBuffer sbQ = new StringBuffer("<table border=\"0\" >");
		sbQ.append("<tr>");
		if(this.getOperation().equals(IHandlerConstant.ADDITION)) {
			List data = this.getAdddata();
			for(int i=0; i<data.size(); i++) {
				String qData = (String) data.get(i);
				sbQ.append(generateTd(qData));
				sbQ.append("<td class=\"data\">");
				if(i+1 < data.size()) {
					sbQ.append(" ");
					sbQ.append(this.getOperationsign());
					sbQ.append(" ");
				}
				else {
					sbQ.append(" = ");
				}
				sbQ.append("</td>");
			}
		}
		else {
			sbQ.append(generateTd(NumberFormatter.optionalFormat(this.getData1(), format)));
			sbQ.append("<td class=\"data\">");
			sbQ.append(" ");
			sbQ.append(this.getOperationsign());
			sbQ.append(" ");	
			sbQ.append("</td>");
			sbQ.append(generateTd(NumberFormatter.optionalFormat(this.getData2(), format)));
			sbQ.append(" ");
			sbQ.append("<td class=\"data\">");
			sbQ.append(" = ");
			sbQ.append("</td>");

		}
		sbQ.append("<td class=\"data\">");
		sbQ.append(NumberFormatter.format(getTempValue(), format));
		sbQ.append("</td>");
		
		sbQ.append("</tr>");
		sbQ.append("</table>");		
		return sbQ.toString();
	}

	
	//---------------------------------------------------------------------------------------------------
	// Private methods
    //---------------------------------------------------------------------------------------------------	
	/**
	 * @param sbQ
	 * @param qData
	 */
	private String generateTd(String qData) {
		StringBuffer sbQ = new StringBuffer();
		if(qData.equals("?")) {
			sbQ.append("<td class=\"question\">");
			sbQ.append(qData);
		}
		else {
			sbQ.append("<td class=\"data\">");
			String format = NumberFormatter.getFormat(getDecimalDigits());
			sbQ.append(NumberFormatter.format(qData, format));
		}
		sbQ.append("</td>");
		return sbQ.toString();
	}	
	
	private void generateData() {
		
		if(getOperation().equals(IHandlerConstant.ADDITION)) {
			List data = getAdddata();
			int questionpos = 0;
			if(data.size() == 2) {
				if(isreverse) {
					questionpos = 1;
					isreverse = false;
				}
				else {
					questionpos = 2;
					isreverse = true;
				}
			}
			else {
				questionpos = RandomNumberGenerator.generate(1);
				while(questionpos > data.size()) {
					questionpos = RandomNumberGenerator.generate(1);
				}				
			}

				String answer = getAnswer();
				setTempValue(answer);
				String value = (String) data.get(questionpos-1);
				setAnswer(value);
				data.remove(questionpos-1);
				data.add(questionpos-1, "?");
			//}

		}
		else if(getOperation().equals(IHandlerConstant.SUBTRACTION) ||
				getOperation().equals(IHandlerConstant.MULTIPLICATION)) {
			resetData(this, null, null);
		}
		
	}
	
	/**
	 * @param sumdetail
	 * @param data
	 */
	private void resetData(AlgebraProcessHandler sumdetail, String data1, String data2) {
		String inbuiltData1 = data1;
		String inbuiltData2 = data2;
		if(inbuiltData1 == null) {
			inbuiltData1 = sumdetail.getData1();
		}
		if(inbuiltData2 == null) {
			inbuiltData2 = sumdetail.getData2();
		}		
		String answer = sumdetail.getAnswer();
		sumdetail.setTempValue(answer);
		if(isreverse) {
			sumdetail.setAnswer(inbuiltData2);
			sumdetail.setData2("?");
			sumdetail.setData1(inbuiltData1);
			isreverse = false;
		}
		else {
			sumdetail.setAnswer(inbuiltData1);
			sumdetail.setData2(inbuiltData2);
			sumdetail.setData1("?");
			isreverse = true;
		}
	}	
} //AlgebraProcessHandler
